<template>
  <div class="userManage">
    <search-form
      ref="header"
      :search-data="ruleForm"
      :search-form="searchData"
      :search-handle="searchHandle"
    ></search-form>
    <base-table
      :tableData="tableData"
      :tableColumn="tableTitle"
      :isPage="false"
      :height="tableHeight"
      :tableOption="tableOption"
      @action="handleButton"
    >
      <template v-slot:label="slotData">
        <span>
          <el-tag
            v-for="tag in slotData.data.row.label"
            :key="tag"
            :type="tag"
            style="margin:0 5px 5px;"
          >
            {{ tag }}
          </el-tag>
        </span>
      </template>
    </base-table>

    <!-- 添加或编辑弹框 -->
    <addOrEidtDialog
      ref="userDialog"
      v-if="dialogObj.show"
      :Visible="dialogObj.show"
      :type="dialogObj.type"
      :dialogRow="dialogObj.dialogRow"
      @close="dialogObj.show = false"
      @refresh="getListData"
    ></addOrEidtDialog>
  </div>
</template>
<script>
import baseTable from "@/components/Table/index.vue";
import searchForm from "@/components/SearchForm/index.vue";
import addOrEidtDialog from "./component/addOrEditDialog.vue";
import { delSystemList, getSystemList } from "@/api/system";
import { useStore } from "vuex";
import { getHeatStation } from "@/api/heatStation";
import { reactive, toRefs, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import heightMinxin from "@/utils/tableHeight.mixin";
export default {
  name: "system",
  mixins: [heightMinxin],
  components: { searchForm, baseTable, addOrEidtDialog },
  setup() {
    const store = useStore();
    const state = reactive({
      ruleForm: {
        heatSourceId: "",
        heatStationId: "",
      },
      searchData: [
        {
          type: "Select",
          prop: "heatSourceId",
          placeholder: "选择热源",
          options: [],
          change: (val) => {
            getHeatStationList(val);
          },
        },
        {
          type: "Select",
          prop: "heatStationId",
          placeholder: "选择换热站",
          options: [],
          change: (row) => {},
        },
      ],
      searchHandle: [
        {
          label: "搜索",
          type: "primary",
          handle: () => {
            getListData();
          },
        },
        {
          label: "添加",
          handle: () => {
            handleAdd("add", state.ruleForm);
          },
        },
      ],
      tableData: [],
      tableTitle: [
        { prop: "heatSourceName", label: "热源名称" },
        { prop: "heatStationName", label: "换热站名称" },
        { prop: "name", label: "系统名称" },
        // { prop: "code", label: "系统编码" },
        { prop: "userType", label: "用户类型" },
        { prop: "heatModel", label: "供热模式" },
        { prop: "heatingType", label: "节能类型" },
        {
          prop: "heatingArea",
          label: "供热面积",
        },
        {
          prop: "lowArea",
          label: "低温面积",
        },
        {
          prop: "longitude",
          label: "经度",
        },
        {
          prop: "latitude",
          label: "纬度",
        },
        {
          prop: "label",
          label: "标签",
          columnType: "slot",
          slotName: "label",
        },
      ],
      tableOption: {
        label: "操作",
        buttons: [
          { name: "编辑", methods: "edit" },
          { name: "删除", methods: "delete", style: { color: "red" } },
        ],
      },
      dialogObj: { show: false, type: "add", dialogRow: {} },
    });

    onMounted(async () => {
      await getHeatSourceList();
      getListData();
    });
    /**热源 */
    const getHeatSourceList = () => {
      let list = store.getters.sourceData;
      state.searchData[0].options = list;
      state.ruleForm.heatSourceId = list ? list[0].value : "";
      getHeatStationList(state.ruleForm.heatSourceId);
    };
    /**
     * 根据热源选择换热站
     */
    const getHeatStationList = async (id) => {
      state.ruleForm.heatStationId = "";
      state.searchData[1].options = [];
      if (id == "" || id == null || id == undefined) {
        state.ruleForm.heatStationId = "";
        state.searchData[1].options = [];
        return;
      }
      const { data: resp } = await getHeatStation({ heatSourceId: id });
      if (resp.success) {
        const list = resp.data.map((item) => {
          return { label: item.name, value: item.id };
        });
        state.searchData[1].options = list;
      }
    };
    /**
     * 查询
     */
    const getListData = async () => {
      const { data: res } = await getSystemList(state.ruleForm);
      if (res.success) {
        if (res.data) {
          const result = res.data.map((item) => {
            item.label = item.label ? JSON.parse(item.label) : [];
            return item;
          });
          state.tableData = result;
        } else {
          state.tableData = [];
        }
      }
    };
    /***
     * 添加
     */
    const handleAdd = (type, row) => {
      state.dialogObj.show = true;
      state.dialogObj.type = type;
      state.dialogObj.dialogRow = row;
    };
    /**
     * 编辑和删除
     */
    const handleButton = ({ row, methods, index }) => {
      if (methods == "edit") {
        handleAdd(methods, row);
      } else {
        handleDelete(row, index);
      }
    };

    const handleDelete = (row, index) => {
      ElMessageBox.confirm(`确定删除该系统 - ${row.name}?`, "提示", {
        cancelButtonText: "取消",
        confirmButtonText: "确定",
        type: "warning",
      })
        .then(() => {
          delSystemList({ id: row.id }).then((res) => {
            if (res.data.success) {
              state.tableData.splice(index, 1);
              ElMessage.success(res.data.msg);
            }
          });
        })
        .catch(() => {});
    };

    return {
      ...toRefs(state),
      handleButton,
      getListData,
    };
  },
};
</script>
